/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.controller;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;

import javax.swing.JEditorPane;
import javax.swing.JTree;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;
import javax.swing.text.html.HTMLDocument;
import javax.swing.tree.DefaultMutableTreeNode;

import offset.nodes.client.editor.Editor;
import offset.nodes.client.editor.controller.EditorActions.AbstractEditorAction;
import offset.nodes.client.editor.view.PopupTree;
import offset.nodes.client.editor.view.StyledDocumentWriter;
import offset.nodes.client.editor.view.StyledHTMLWriter;

/**
 *
 * @author Walter Lütgenau
 */
public class ViewActions {

    public static abstract class AbstractViewAction extends AbstractEditorAction {

        public AbstractViewAction(String name, Editor editor) {
            super(name, editor);
        }
    }

    /*
    public static class ToggleSourceViewAction extends AbstractViewAction {

        boolean isBrowser = true;

        public ToggleSourceViewAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("Source"), editor);
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/sc_htmlview.png")));
        }

        protected void setupBrowserPane() {
            getEditor().getBrowserPane().getDocument().addUndoableEditListener(editor.getUndoableEditListener());
            getEditor().getHeading().update();
            getEditor().getStructure().update();
        }

        public void action(ActionEvent e) {
            try {
                if (getEditor().getToggleSourceMenuItem().isSelected()) {
                    getEditor().getSourcePane().getDocument().remove(0, getEditor().getSourcePane().getDocument().getLength());
                    StyledHTMLWriter writer = new StyledHTMLWriter(new StyledDocumentWriter((DefaultStyledDocument) getEditor().getSourcePane().getDocument()), (HTMLDocument) getEditor().getBrowserPane().getDocument());
                    writer.write();
                    getEditor().getSourcePane().setCaretPosition(0);
                    getEditor().getSourcePane().grabFocus();
                    ((CardLayout) getEditor().getMainPanel().getLayout()).show(getEditor().getMainPanel(), "source");
                    isBrowser = false;
                } else {
                    if (getEditor().isDebug()) {
                        getEditor().getBrowserPane().setText(getEditor().getSourcePane().getText());
                        setupBrowserPane();
                        getEditor().getBrowserPane().setCaretPosition(0);
                    }
                    getEditor().getBrowserPane().grabFocus();
                    ((CardLayout) getEditor().getMainPanel().getLayout()).show(getEditor().getMainPanel(), "browser");
                    isBrowser = true;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }*/

    public static class SourceViewAction extends AbstractViewAction {

        boolean isBrowser = true;

        public SourceViewAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("Source"), editor);
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/sc_htmlview.png")));
        }

        protected void setupBrowserPane() {
            getEditor().getBrowserPane().getDocument().addUndoableEditListener(editor.getUndoableEditListener());
            getEditor().getHeading().update();
            getEditor().getStructure().update();
        }

        public void action(ActionEvent e) {
            try {
                getEditor().getSourcePane().getDocument().remove(0, getEditor().getSourcePane().getDocument().getLength());
                StyledHTMLWriter writer = new StyledHTMLWriter(new StyledDocumentWriter((DefaultStyledDocument) getEditor().getSourcePane().getDocument()), (HTMLDocument) getEditor().getBrowserPane().getDocument());
                writer.write();
                getEditor().getSourcePane().setCaretPosition(0);
                getEditor().getSourcePane().grabFocus();
                ((CardLayout) getEditor().getMainPanel().getLayout()).show(getEditor().getMainPanel(), "source");
                isBrowser = false;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static class EditorViewAction extends AbstractViewAction {

        boolean isBrowser = true;

        public EditorViewAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("Editor"), editor);
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/editor.png")));
        }

        protected void setupBrowserPane() {
            getEditor().getBrowserPane().getDocument().addUndoableEditListener(editor.getUndoableEditListener());
            getEditor().getHeading().update();
            getEditor().getStructure().update();
        }

        public void action(ActionEvent e) {
            try {
                if (getEditor().isDebug()) {
                    getEditor().getBrowserPane().setText(getEditor().getSourcePane().getText());
                    setupBrowserPane();
                    getEditor().getBrowserPane().setCaretPosition(0);
                }
                getEditor().getBrowserPane().grabFocus();
                ((CardLayout) getEditor().getMainPanel().getLayout()).show(getEditor().getMainPanel(), "browser");
                isBrowser = true;

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static class HeadingViewAction extends AbstractViewAction {

        public HeadingViewAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("view.headings"), editor);
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/sc_heading_1.png")));
        }

        public void action(ActionEvent e) {
            ((CardLayout) getEditor().getTreePanel().getLayout()).show(getEditor().getTreePanel(), "headings");
        }
    }

    public static class StructureViewAction extends AbstractViewAction {

        PopupTree.NodePopup[] nodePopups;

        public StructureViewAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("view.structure"), editor);
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/sc_structure.png")));
        }

        public void action(ActionEvent e) {
            getEditor().getStructureScroll().setViewportView(getEditor().getStructureTree());
            ((CardLayout) getEditor().getTreePanel().getLayout()).show(getEditor().getTreePanel(), "structure");
        }
    }

    public static class SelectElementAction extends AbstractEditorAction {

        JEditorPane pane;
        JTree tree;

        public SelectElementAction(JEditorPane pane, JTree tree, Editor editor) {
            super("", editor);
            this.pane = pane;
            this.tree = tree;
        }

        protected void selectNode() {
            if (tree.getSelectionPath() == null)
                return;

            DefaultMutableTreeNode remove = (DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent();

            Element element = (Element) remove.getUserObject();

            try {
                scrollToDocumentPosition(pane, element.getStartOffset());
                pane.setSelectionStart(element.getStartOffset());
                pane.setSelectionEnd(element.getEndOffset() - 1);
                pane.grabFocus();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void action(ActionEvent e) throws Exception {
            selectNode();
        }
    }
}
